var api = require('../../../utils/api.js')
var $ = require('../../../utils/util.js')
var regeneratorRuntime = require('../../../utils/runtime.js')
var app = getApp()
var Store = app.Store 

Page({
  data: {
    basic:{
      operationType:0,
      ticketNumber:'',
      acceptorName:'', 
      acceptorId:'', 
      operatorName:'', 
      operatorId:'', 
      ticketIssuerName:'',
      ticketIssuerId:'',
      deptId:'',
      deptName:'',
      stationName:'',
      stationId:'',
      deviceName:'',
      deviceId:'',
      operationContent:'',
      operationTask:'',
      relatedTasks:'',
      operationStatus:'',
      operationTasksList:[]
    },
    operateList:[],
    info:'',
    isShowTime: false, // 是否显示时间弹框
    isShowTime2: false, // 是否显示时间弹框
    submit: '', // 保存0；提交1 
    type: '', // 类型
  },
  onLoad: function (options) { 
    const userName=wx.getStorageSync('user_name')
    const userId=wx.getStorageSync('user_id')
    if ('ticketInfo' in options){ 
      const ticketInfo=JSON.parse(options.ticketInfo)
      console.log(ticketInfo)
      this.setData({
        "basic":ticketInfo, // 区分接口
        "basic.operationType":options.operationType,
        'basic.ticketIssuerName':userName,
        'basic.ticketIssuerId':userId,
        'basic.relatedTasks':ticketInfo.ticketNumber,
        'basic.ticketNumber':'',
        'basic.deptName':ticketInfo.relatedInformation.projectName,
        'basic.deptId':ticketInfo.relatedInformation.projectDeptId,
        'basic.deviceName':ticketInfo.relatedInformation.deviceName,
        'basic.deviceId':ticketInfo.relatedInformation.deviceId,
        'basic.operationContent':ticketInfo.stationName+ticketInfo.relatedInformation.deviceName+'由运行转检修',
        'basic.operationTask':ticketInfo.stationName+ticketInfo.relatedInformation.deviceName+'由运行转检修',
        'basic.operationTasksList':[],
        'type':'workAdd'
      })
      this.getTickNumber(ticketInfo.stationId)
    }else if('ticketNumber' in options){
      api.$get(api.operateTicket.operateTickInfo+options.ticketNumber).then((res) => {
        this.setData({
          basic:res.data,
          'basic.deptName':res.data.relatedInformation.projectName,
          'basic.deptId':res.data.relatedInformation.projectDeptId,
          'basic.deviceName':res.data.relatedInformation.deviceName,
          'basic.deviceId':res.data.relatedInformation.deviceId,
          'basic.operationTasksList':res.data.operationTasksList,
          type:'edit',
        })
      })
    }else{
      this.setData({
        'basic.ticketIssuerName':userName,
        'basic.ticketIssuerId':userId,
        "basic.operationType":options.operationType,
        'type':'add'
      })
    }
    
    
  },

  onShow(){
    var form = Store.getState().formInfor.form
    var key = form.key
    if (!form.selected) return
    switch (key) {
      case 'selectOrderPeop': // 获取受令人  
        this.setData({
          "basic.acceptorName": form.selected.userName,
          "basic.acceptorId": form.selected.userId,
        })
        break
      case 'selectOperator': // 获取操作人  
        this.setData({
          "basic.operatorName": form.selected.userName,
          "basic.operatorId": form.selected.userId,
        })
        break
      case 'selectWorkProject'://获取项目
        this.setData({
          "basic.deptName": form.selected.label,
          "basic.deptId":form.selected.id,
          "basic.relatedInformation.projectDeptId":form.selected.id
        })
        break
      case 'selectWorkStation': // 获取厂站  
        this.setData({
          "basic.stationName": form.selected.label,
          "basic.stationId": form.selected.station,
        })
        this.getTickNumber(form.selected.station)
        break
      case 'selectWorkDevice': // 获取设备  
        this.setData({
          "basic.deviceName": form.selected.label,
          "basic.deviceId": form.selected.id,
          "basic.workAddr":this.data.basic.stationName+form.selected.label,
          "basic.relatedInformation.deviceId":form.selected.id
        });
        break
      case 'getOperateProcess': // 获取操作列表  
        this.setData({
          "operateList": form.selected,
        })
        break
      case 'operateSign': // 获取签发人签名  
        this.setData({
          responsibleImg: form.selected,
        })
        break 
        
    }
    console.log(form,this.data.responsibleImg) 
  },
  getTickNumber(stationId){
    if(this.data.type!='edit'){
      api.$get(api.operateTicket.requestTicketNumber,{stationId:stationId}).then((res) => {
        this.setData({'basic.ticketNumber':res.data.ticketNumber})
      })
    }
  },
  selectWorkProject(){
    if (this.data.type!='add'&&this.data.basic.operationStatus==6) {
      $.toast('已生成票号无法修改，如需换站请重新新增操作票')
      return false
    }
    Store.dispatch({
      type: 'SET_FROM',
      form: {
        key: 'selectWorkProject', // 后台要的更改字段 
        type: 1, // 表单类型 1： radio  2：select
      },
    })
    $.linkurl(`/pages/main/chooseForm/chooseForm`)
  },
  selectWorkStation(){
    if (this.data.type!='add'&&this.data.basic.operationStatus==6) {
      $.toast('已生成票号无法修改，如需换站请重新新增操作票')
      return false
    }
    if (this.data.basic.deptName == '') {
      $.toast('请选择项目')
      return false
    }
    Store.dispatch({
      type: 'SET_FROM',
      form: {
        key: 'selectWorkStation', // 后台要的更改字段 
        type: 1, // 表单类型 1： radio  2：select
        config: {
          parentId: this.data.basic.deptId
        },
      },
    })
    $.linkurl(`/pages/main/chooseForm/chooseForm`)
  },
  selectWorkDevice (e) { // 根据厂站获取设备列表 
    if (this.data.type!='add'&&this.data.basic.operationStatus==6) {
      $.toast('已生成票号无法修改，如需换站请重新新增操作票')
      return false
    }
    if (this.data.basic.stationName == '') {
      $.toast('请选择厂站')
    } else {
      Store.dispatch({
        type: 'SET_FROM',
        form: {
          key: 'selectWorkDevice', // 后台要的更改字段 
          type: 1, // 表单类型 1： radio  2：select
          config: {
            stationId: this.data.basic.stationId
          },
        },
      })
      $.linkurl(`/pages/main/chooseForm/chooseForm`)
      
    } 
  },
  selectOrderPeop() { // 受令人 
    if(this.data.info.submit=='1'){
      $.toast("已提交无法修改")
    }else{
      Store.dispatch({
        type: 'SET_FROM',
        form: {
          key: 'selectOrderPeop', // 后台要的更改字段 
          type:1, // 表单类型 1： radio  2：select
          config: {
            // deptId: this.data.basic.deptId ,
            deptId:0
          },
        },
      })
      $.linkurl(`/pages/main/chooseForm/chooseForm`)
    }
  },
  selectOperator(){
    if(this.data.info.submit=='1'){
      $.toast("已提交无法修改")
    }else{
      Store.dispatch({
        type: 'SET_FROM',
        form: {
          key: 'selectOperator', // 后台要的更改字段 
          type:1, // 表单类型 1： radio  2：select
          config: {
            // deptId: this.data.basic.deptId,
            deptId:0,
          },
        },
      })
      $.linkurl(`/pages/main/chooseForm/chooseForm`)
    }
  },
  getOperateProcess(e){//暂时不用
    const {type}=$.detail(e)
    Store.dispatch({
      type: 'SET_FROM',
      form: {
        key: 'getOperateProcess', // 后台要的更改字段 
        type:'0',
        config: {
          deviceName:this.data.basic.deviceName, 
          deviceId: this.data.basic.deviceId,
        },
      },
    })
    $.linkurl(`/pages/operationTicket/operationContent/operationContent?deviceId=${this.data.basic.deviceId}&ticketNumber=${this.data.basic.ticketNumber}&deptId=${this.data.basic.relatedInformation.projectDeptId}&type=${this.data.type}`) 
  },
  bigSign(e){
    const {img}=$.detail(e)
    wx.previewImage({
      current: '', // 当前显示图片的链接，不填则默认为urls的第一张
      urls: [img] // 需要预览的图片链接列表
    })
  },
  responsibleSign(e){  
    Store.dispatch({
      type: 'SET_FROM',
      form: {
        key: 'operateSign', 
      },
    })
    $.linkurl(`/pages/components/signatrue/signAture`) 
  },
  cancelMake(){
    $.linkurl('','back')
  },
  subTicket(){
    // console.log(this.data.basic.operationTasksList)
    if(this.data.basic.deviceName==''||this.data.basic.content==''||this.data.basic.acceptorName==''||this.data.basic.operatorName==''){
      $.toast('请输入完整的必填项信息')
      return false
    }
    (this.data.type=='edit'?api.$put(api.operateTicket.editTicketOperation,{...this.data.basic}):api.$post(api.operateTicket.addTicketOperation,{...this.data.basic})).then((res) => {
      if(res.code==200){
        $.linkurl(`/pages/operationTicket/operationContent/operationContent?deviceId=${this.data.basic.deviceId}&ticketNumber=${this.data.basic.ticketNumber}&deptId=${this.data.basic.deptId}&type=${this.data.type}&operationStatus=${this.data.basic.operationStatus}&operationTasksList=${this.data.basic.operationTasksList.length}&operatorId=${this.data.basic.operatorId}`,'redirect')
      }
    })
    // $.linkurl(`/pages/operationTicket/riskManage/riskManage?director=${this.data.basic.workCharger}&reciveOrder=${this.data.basic.reciveOrder}&operateContent=${this.data.basic.operateContent}`)
  },


  input_handle(e){
    this.setData({
      [e.currentTarget.dataset.key]: e.detail.value,
      'basic.operationTask':e.detail.value
    })
  },
})